package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @Auther: yangjin
 * @Date: 2021/6/1 21:22
 */
@RestController
@Slf4j
public class OrderController {
    public static final String PaymentSrv_URL = "http://CLOUD-PAYMENT-SERVICE";

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/consumer/payment/create") //客户端用浏览器是get请求，但是底层实质发送post调用服务端
    public CommonResult create(@RequestBody Payment payment)
    {
        System.err.println(payment.getSerial()+"============");
        return restTemplate.postForObject(PaymentSrv_URL + "/payment/create",payment, CommonResult.class);
    }

    @PostMapping("/consumer/payment/createForEntity") //客户端用浏览器是get请求，但是底层实质发送post调用服务端
    public CommonResult createForEntity(@RequestBody Payment payment)
    {
        System.err.println(payment.getSerial()+"============");
        return restTemplate.postForEntity(PaymentSrv_URL + "/payment/create",payment, CommonResult.class).getBody();
    }


    /**
     * getForObject：返回对象为响应体中数据转化成的对象，基本上可以理解为Json
     * getForEntity：返回对象为ResponseEntity对象，包含了响应中的一些重要信息，比如响应头、响应状态码、响应体等
     */
    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPayment(@PathVariable Long id)
    {
        System.out.println("go into orderController");
        return restTemplate.getForObject(PaymentSrv_URL + "/payment/get/"+id, CommonResult.class, id);
    }

    @GetMapping("/consumer/payment/getForEntity/{id}")
    public CommonResult getPaymentByForEntity(@PathVariable Long id)
    {
        System.out.println("go into orderController");
        return restTemplate.getForEntity(PaymentSrv_URL + "/payment/get/"+id, CommonResult.class, id).getBody();
    }
}
